1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   */
19  package org.codehaus.groovy.control.customizers;
20  
21  import groovy.lang.Closure;
22  import org.codehaus.groovy.ast.ClassNode;
23  import org.codehaus.groovy.classgen.GeneratorContext;
24  import org.codehaus.groovy.control.CompilationFailedException;
25  import org.codehaus.groovy.control.SourceUnit;
26  import org.codehaus.groovy.control.io.FileReaderSource;
27  import org.codehaus.groovy.control.io.ReaderSource;
28  
29  /**
30   * A base class for customizers which only have to be applied on specific source units.
31   * This is for example useful if you want a customizer to be applied only for files
32   * matching some extensions.
33   * <p>
34   * For convenience, this class implements several methods that you may extend to customize
35   * the behaviour of this utility. For example, if you want to apply a customizer only
36   * for classes matching the '.foo' file extension, then you only have to override the
37   * {@link #acceptExtension(String)} method:
38   * <pre><code>return "foo".equals(extension)</code></pre>
39   *
40   * @since 2.1.0
41   * @author Cedric Champeau
42   */
43  public class SourceAwareCustomizer extends DelegatingCustomizer {
44  
45      private Closure<Boolean> extensionValidator;
46      private Closure<Boolean> baseNameValidator;
47      private Closure<Boolean> sourceUnitValidator;
48      private Closure<Boolean> classValidator;
49  
50      public SourceAwareCustomizer(CompilationCustomizer delegate) {
51          super(delegate);
52      }
53  
54      @Override
55      public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException {
56          String fileName = source.getName();
57          ReaderSource reader = source.getSource();
58          if (reader instanceof FileReaderSource) {
59              FileReaderSource file = (FileReaderSource) reader;
60              fileName = file.getFile().getName();
61          }
62          if (acceptSource(source) && acceptClass(classNode) && accept(fileName)) {
63              delegate.call(source, context, classNode);
64          }
65      }
66  
67      public void setBaseNameValidator(final Closure<Boolean> baseNameValidator) {
68          this.baseNameValidator = baseNameValidator;
69      }
70  
71      public void setExtensionValidator(final Closure<Boolean> extensionValidator) {
72          this.extensionValidator = extensionValidator;
73      }
74  
75      public void setSourceUnitValidator(final Closure<Boolean> sourceUnitValidator) {
76          this.sourceUnitValidator = sourceUnitValidator;
77      }
78  
79      public void setClassValidator(final Closure<Boolean> classValidator) {
80          this.classValidator = classValidator;
81      }
82  
83      public boolean accept(String fileName) {
84          int ext = fileName.lastIndexOf(".");
85          String baseName = ext<0?fileName:fileName.substring(0, ext);
86          String extension = ext<0?"":fileName.substring(ext+1);
87          return acceptExtension(extension) && acceptBaseName(baseName);
88      }
89  
90      public boolean acceptClass(ClassNode cnode) {
91          return classValidator == null || classValidator.call(cnode);
92      }
93  
94      public boolean acceptSource(SourceUnit unit) {
95          return sourceUnitValidator==null || sourceUnitValidator.call(unit);
96      }
97  
98      public boolean acceptExtension(String extension) {
99          return extensionValidator==null || extensionValidator.call(extension);
100     }
101 
102     public boolean acceptBaseName(String baseName) {
103         return baseNameValidator==null || baseNameValidator.call(baseName);
104     }
105 }